﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.WebNemo.Business.Groups;
using System.Web.UI.WebControls;
using System.Text;

namespace blackhouse.WebNemo.Web.Controls {
	public class ReferenceEditRecordValue : EditRecordValue {

		#region Fields

		Panel controlPanel;
		HiddenField field;
		Literal literal;

		#endregion

		#region Properties

		public override bool DisplayInline {
			get { return false; }
		}

		#endregion

		#region Methods

		protected override void CreateChildControls() {
			base.CreateChildControls();

			this.controlPanel = new Panel();
			this.Controls.Add(controlPanel);
			this.field = new HiddenField();
			controlPanel.Controls.Add(field);
			this.literal = new Literal();
			controlPanel.Controls.Add(literal);
		}

		protected override void PopulateControl() {
			this.EnsureChildControls();


			GroupRecord[] records;

			if (this.column.ColumnType == FieldType.ManyReference)
				records = this.record.GetRecords(this.column.ColumnName);
			else {
				if (this.record.IsNotNull(this.column.ColumnName))
					records = new GroupRecord[] { this.record.GetRecord(this.column.ColumnName) };
				else
					records = new GroupRecord[0];
			}
			StringBuilder sb = new StringBuilder();
			List<int> refIds = new List<int>();

			this.controlPanel.Attributes.Add("wrap-group", this.column.RelatedGroupId.ToString());
			sb.Append("<div class=\"record-refs\">");
			foreach (GroupRecord record in records) {
				sb.AppendFormat("<a class=\"record-ref\" href=\"{2}\" itemid=\"{0}\">{1}<span></span></a><br />", record.RecordId, record.Identyficator, NemoUrlUtils.GetUrlUtils().GetDisplayRecord(record.BelongsToGroupId, record.RecordId));
				refIds.Add(record.RecordId);
			}
			sb.Append("</div>");

			string text = "przypisania";
			if (this.column.ColumnType == FieldType.OneReference)
				text = "przypisanie";

			sb.AppendFormat("<div wrap-group-actions=\"{2}\" class=\"edit-record-refa\"><a href=\"{5}\">Edytuj {4}</a><a href=\"remove-reference-{0}-{1}-{2}-{3}\">Usuń {4}</a></div>", this.record.BelongsToGroupId, this.record.RecordId, this.column.RelatedGroupId, this.column.ColumnName, text, NemoUrlUtils.GetUrlUtils().GetEditReference(this.record.BelongsToGroupId, this.record.RecordId, this.column.RelatedGroupId, this.column.ColumnName, new int[0]));

			this.field.Value = string.Join(",", refIds.Select(i => i.ToString()).ToArray());
			this.literal.Text = sb.ToString();

		}

		public override void SaveInRecord() { 
			List<int> selectedRefs = new List<int>();
			foreach (string srf in this.Page.Request[this.field.UniqueID].Split(',')) {
				int srfi;
				if (Int32.TryParse(srf, out srfi))
					selectedRefs.Add(srfi);
			}

			if (this.column.ColumnType == FieldType.OneReference) {
				int v = 0;
				if (selectedRefs.Count == 1)
					this.record.SetValue(this.column.ColumnName, v);
				else
					this.record.SetNullValue(this.column.ColumnName);
			}
			else {
				this.record.SetRecords(this.column.ColumnName, selectedRefs.ToArray());
			}
		}

		#endregion

	}
}